home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk33 / txtrdr / textreader.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  12KB  |  642 lines

  1. /* TextReader by Oren Peli, Copyright (c) 1987 BazboSOFT! */
  2.  
  3. #include <intuition/intuitionbase.h>    /* Added by Adams Douglas */
  4. #include <devices/conunit.h>
  5. #include <devices/input.h>
  6. #include <exec/memory.h>
  7. #include <graphics/gfxbase.h>
  8. #include <libraries/dosextens.h>
  9.  
  10. extern void *AllocMem();
  11.  
  12. extern PLANEPTR AllocRaster();
  13.  
  14. struct IntuitionBase *IntuitionBase = NULL;
  15. struct GfxBase *GfxBase = NULL;
  16.  
  17. extern void *OpenLibrary();
  18.  
  19. extern void *CreateStdIO();
  20.  
  21. extern long findWindow();
  22.  
  23. struct ConUnit *conUnit = NULL;
  24.  
  25. struct Window *conWindow = NULL;
  26.  
  27. void *CreatePort() , *FindPort();
  28.  
  29. struct Process *FindTask();
  30.  
  31. struct IntuiMessage *GetMsg();
  32.  
  33. struct RastPort dummy_rastport , *trp = &dummy_rastport;
  34. struct BitMap dummy_bitmap , *tbm = &dummy_bitmap;
  35.  
  36. struct RastPort dummy_rastport2 , *crp = &dummy_rastport2;
  37. struct BitMap dummy_bitmap2 , *cbm = &dummy_bitmap2;
  38.  
  39.  
  40. struct IntuiText ItemText = { 3 , 1 , JAM1 , 1 , 1 , NULL , (STRPTR) "Quit TextReader"
  41. , NULL };
  42.  
  43. struct MenuItem MenuItem = { NULL , 0 , 0 , 121 , 10 , ITEMTEXT | ITEMENABLED |
  44. HIGHCOMP, 0, (APTR) &ItemText , NULL , NULL , NULL , 0xFFFF };
  45.  
  46. struct Menu Menu = { NULL , 0 , 0 , 121 , 0 , MENUENABLED , " Quit" , &MenuItem };
  47.  
  48. struct chr_data
  49. {
  50.     char chr;
  51.     char raw_data;
  52.     char is_shifted;
  53.     PLANEPTR pln;
  54. };
  55.  
  56. struct chr_data chrs[] =
  57. {
  58.     {    ' '    ,    0x40    ,    0    } ,
  59.     {    'a'    ,    0x20    ,    0    } ,
  60.     {    'b'    ,    0x35    ,    0    } ,
  61.     {    'c'    ,    0x33    ,    0    } ,
  62.     {    'd'    ,    0x22    ,    0    } ,
  63.     {    'e'    ,    0x12    ,    0    } ,
  64.     {    'f'    ,    0x23    ,    0    } ,
  65.     {    'g'    ,    0x24    ,    0    } ,
  66.     {    'h'    ,    0x25    ,    0    } ,
  67.     {    'i'    ,    0x17    ,    0    } ,
  68.     {    'j'    ,    0x26    ,    0    } ,
  69.     {    'k'    ,    0x27    ,    0    } ,
  70.     {    'l'    ,    0x28    ,    0    } ,
  71.     {    'm'    ,    0x37    ,    0    } ,
  72.     {    'n'    ,    0x36    ,    0    } ,
  73.     {    'o'    ,    0x18    ,    0    } ,
  74.     {    'p'    ,    0x19    ,    0    } ,
  75.     {    'q'    ,    0x10    ,    0    } ,
  76.     {    'r'    ,    0x13    ,    0    } ,
  77.     {    's'    ,    0x21    ,    0    } ,
  78.     {    't'    ,    0x14    ,    0    } ,
  79.     {    'u'    ,    0x16    ,    0    } ,
  80.     {    'v'    ,    0x34    ,    0    } ,
  81.     {    'w'    ,    0x11    ,    0    } ,
  82.     {    'x'    ,    0x32    ,    0    } ,
  83.     {    'y'    ,    0x15    ,    0    } ,
  84.     {    'z'    ,    0x31    ,    0    } ,
  85.     {    '0'    ,    0x0a    ,    0    } ,
  86.     {    '1'    ,    0x01    ,    0    } ,
  87.     {    '2'    ,    0x02    ,    0    } ,
  88.     {    '3'    ,    0x03    ,    0    } ,
  89.     {    '4'    ,    0x04    ,    0    } ,
  90.     {    '5'    ,    0x05    ,    0    } ,
  91.     {    '6'    ,    0x06    ,    0    } ,
  92.     {    '7'    ,    0x07    ,    0    } ,
  93.     {    '8'    ,    0x08    ,    0    } ,
  94.     {    '9'    ,    0x09    ,    0    } ,
  95.     {    '-'    ,    0x0b    ,    0    } ,
  96.     {    '='    ,    0x0c    ,    0    } ,
  97.     {    '\\'    ,    0x0d    ,    0    } ,
  98.     {    '['    ,    0x1a    ,    0    } ,
  99.     {    ']'    ,    0x1b    ,    0    } ,
  100.     {    ';'    ,    0x29    ,    0    } ,
  101.     {    '\''    ,    0x2a    ,    0    } ,
  102.     {    ','    ,    0x38    ,    0    } ,
  103.     {    '.'    ,    0x39    ,    0    } ,
  104.     {    '/'    ,    0x3a    ,    0    } ,
  105.     {    '`'    ,    0x00    ,    0    } ,
  106.     {    'A'    ,    0x20    ,    1    } ,
  107.     {    'B'    ,    0x35    ,    1    } ,
  108.     {    'C'    ,    0x33    ,    1    } ,
  109.     {    'D'    ,    0x22    ,    1    } ,
  110.     {    'E'    ,    0x12    ,    1    } ,
  111.     {    'F'    ,    0x23    ,    1    } ,
  112.     {    'G'    ,    0x24    ,    1    } ,
  113.     {    'H'    ,    0x25    ,    1    } ,
  114.     {    'I'    ,    0x17    ,    1    } ,
  115.     {    'J'    ,    0x26    ,    1    } ,
  116.     {    'K'    ,    0x27    ,    1    } ,
  117.     {    'L'    ,    0x28    ,    1    } ,
  118.     {    'M'    ,    0x37    ,    1    } ,
  119.     {    'N'    ,    0x36    ,    1    } ,
  120.     {    'O'    ,    0x18    ,    1    } ,
  121.     {    'P'    ,    0x19    ,    1    } ,
  122.     {    'Q'    ,    0x10    ,    1    } ,
  123.     {    'R'    ,    0x13    ,    1    } ,
  124.     {    'S'    ,    0x21    ,    1    } ,
  125.     {    'T'    ,    0x14    ,    1    } ,
  126.     {    'U'    ,    0x16    ,    1    } ,
  127.     {    'V'    ,    0x34    ,    1    } ,
  128.     {    'W'    ,    0x11    ,    1    } ,
  129.     {    'X'    ,    0x32    ,    1    } ,
  130.     {    'Y'    ,    0x15    ,    1    } ,
  131.     {    'Z'    ,    0x31    ,    1    } ,
  132.     {    '!'    ,    0x01    ,    1    } ,
  133.     {    '@'    ,    0x02    ,    1    } ,
  134.     {    '#'    ,    0x03    ,    1    } ,
  135.     {    '$'    ,    0x04    ,    1    } ,
  136.     {    '%'    ,    0x05    ,    1    } ,
  137.     {    '^'    ,    0x06    ,    1    } ,
  138.     {    '&'    ,    0x07    ,    1    } ,
  139.     {    '*'    ,    0x08    ,    1    } ,
  140.     {    '('    ,    0x09    ,    1    } ,
  141.     {    ')'    ,    0x0a    ,    1    } ,
  142.     {    '_'    ,    0x0b    ,    1    } ,
  143.     {    '+'    ,    0x0c    ,    1    } ,
  144.     {    '|'    ,    0x0d    ,    1    } ,
  145.     {    '{'    ,    0x1a    ,    1    } ,
  146.     {    '}'    ,    0x1b    ,    1    } ,
  147.     {    ':'    ,    0x29    ,    1    } ,
  148.     {    '"'    ,    0x2a    ,    1    } ,
  149.     {    '<'    ,    0x38    ,    1    } ,
  150.     {    '>'    ,    0x39    ,    1    } ,
  151.     {    '?'    ,    0x3a    ,    1    } ,
  152.     {    '~'    ,    0x00    ,    1    } ,
  153.     {    -1    ,    -1    ,    -1    }
  154. };
  155.  
  156. struct MsgPort *port = NULL;
  157. struct IOStdReq *req = NULL;
  158.  
  159. long errdev = 0;
  160.  
  161. int num_of_chrs;
  162.  
  163. struct InputEvent ie;
  164.  
  165. int really_open_window = 0;
  166.  
  167. do_args(c , v)
  168. register int c;
  169. register char *v[];
  170. {
  171.     register int i , j , k , s;
  172.     register char chr;
  173.     
  174.     for (i = 1 ; i < c ; i++)
  175.     {
  176.         s = strlen(v[i]);
  177.  
  178.         for (j = 0 ; j < s ; j++)
  179.         {
  180.             if (v[i][j] == '\\' && v[i][j + 1] == 'n')
  181.             {
  182.                 ie.ie_Qualifier = NULL;
  183.  
  184.                 ie.ie_Code = 0x44;
  185.  
  186.                 DoIO(req);
  187.  
  188.                 ie.ie_Code = 0x44 | 0x80;
  189.  
  190.                 DoIO(req);
  191.  
  192.                 j += 2;
  193.             }
  194.  
  195.             chr = v[i][j];
  196.  
  197.             for (k = 0 ; k != num_of_chrs ; k++)
  198.                 if (chrs[k].chr == chr)
  199.                 {
  200.                     fake_raw_key_event(k);
  201.                     
  202.                     break;
  203.                 }
  204.         }
  205.     }
  206. }
  207.  
  208. fake_raw_key_event(c)
  209. register char c;
  210. {
  211.     ie.ie_Qualifier = chrs[c].is_shifted ? IEQUALIFIER_LSHIFT : NULL;
  212.     ie.ie_Code = chrs[c].raw_data;
  213.  
  214.     DoIO(req);
  215.  
  216.     ie.ie_Code |= 0x80;
  217.  
  218.     DoIO(req);
  219. }
  220.  
  221. the_same()
  222. {
  223.     register int i;
  224.  
  225.     register PLANEPTR ptr1 , ptr2;
  226.  
  227.     ptr1 = cbm->Planes[0];
  228.     ptr2 = tbm->Planes[0];
  229.  
  230.     for (i = 0 ; i != 16 ; i += 2)
  231.         if (ptr1[i] != ptr2[i])
  232.             return(0);
  233.  
  234.     return(1);
  235. }
  236.  
  237. identify_char()
  238. {
  239.     register int c;
  240.  
  241.     for (c = 0 ; c != num_of_chrs ; c++)
  242.     {
  243.         cbm->Planes[0] = chrs[c].pln;
  244.  
  245.         if (the_same() )
  246.             return(c);
  247.     }
  248.  
  249.     return(-1);
  250. }
  251.  
  252. convert_char(x , y)
  253. register int x , y;
  254. {
  255.     register int rx , ry;
  256.  
  257.     register int c;
  258.  
  259.     rx = x - conUnit->cu_XROrigin;
  260.     ry = y - conUnit->cu_YROrigin;
  261.  
  262.     rx &= ~7;
  263.     ry &= ~7;
  264.  
  265.     rx += conUnit->cu_XROrigin;
  266.     ry += conUnit->cu_YROrigin;
  267.  
  268.     ClipBlit(conWindow->RPort , (long) rx , (long) ry , trp , 0L , 0L , 8L , 8L ,
  269.                                         0xC0L);
  270.  
  271.     if ( (c = identify_char() ) != -1)
  272.         fake_raw_key_event(c);
  273. }
  274.  
  275. main(argc , argv)
  276. int argc;
  277. char *argv[];
  278. {
  279.     register struct Window *win;
  280.     register struct IntuiMessage *msg;
  281.     register int x , y , code , qua;
  282.     register long class;
  283.     register int pressed = 0;
  284.     register int rx , ry;
  285.     register int prx = -1111 , fy , px = -1111 , maxx = -1111;
  286.     register int i;
  287.     register char buf[2];
  288.     register long bl;
  289.     register char string_buffer[50];
  290.  
  291.     puts("TextReader, by Oren Peli, Copyright (C) 1987 BazboSOFT!");
  292.  
  293.     for (i = 0 ; chrs[i].raw_data != -1 ; i++)
  294.         chrs[i].pln = NULL;
  295.  
  296.     num_of_chrs = i - 1;
  297.  
  298.     if (NOT (IntuitionBase = (struct IntuitionBase *)
  299.                     OpenLibrary("intuition.library" , 33L) ) )
  300.         bye();
  301.  
  302.     if (NOT findWindow() )
  303.         bye();
  304.  
  305.     win = conWindow;
  306.  
  307.     sprintf(string_buffer , "TextReader's port (win:%lx)" , win);
  308.  
  309.     if (FindPort(string_buffer) )
  310.     {
  311.         puts("But you have already ran TextReader from this window!");
  312.  
  313.         bye();
  314.     }
  315.  
  316.     if (NOT (port = CreatePort(string_buffer , 0L) ) )
  317.         bye();
  318.  
  319.     if (NOT (req = CreateStdIO(port) ) )
  320.         bye();
  321.  
  322.     if (errdev = OpenDevice("input.device" , 0L , req , 0L) )
  323.         bye();
  324.  
  325.     if (NOT (GfxBase = (struct GfxBase *) OpenLibrary("graphics.library" , 33L) ) )
  326.         bye();
  327.  
  328.     InitRastPort(trp);
  329.     InitRastPort(crp);
  330.  
  331.     InitBitMap(tbm , 1L , 8L , 8L);
  332.     InitBitMap(cbm , 1L , 8L , 8L);
  333.  
  334.     trp->BitMap = tbm;
  335.     crp->BitMap = cbm;
  336.  
  337.     if (NOT (tbm->Planes[0] = AllocRaster(8L , 8L) ) )
  338.         bye();
  339.  
  340.     SetAPen(trp , 0L);
  341.  
  342.     RectFill(trp , 0L , 0L , 8L , 8L);
  343.  
  344.     buf[1] = 0;
  345.  
  346.     SetAPen(crp , 1L);
  347.     SetDrMd(crp , JAM2);
  348.  
  349.     bl = GfxBase->DefaultFont->tf_Baseline;
  350.  
  351.     for (i = 0 ; i != num_of_chrs ; i++)
  352.     {
  353.         cbm->Planes[0] = chrs[i].pln = AllocRaster(8L , 8L);
  354.  
  355.         if (NOT chrs[i].pln)
  356.             bye();
  357.  
  358.         Move(crp , 0L , bl);
  359.  
  360.         buf[0] = chrs[i].chr;
  361.  
  362.         Text(crp , buf , 1L);
  363.     }
  364.  
  365.     req->io_Command = IND_WRITEEVENT;
  366.     req->io_Flags = 0;
  367.     req->io_Length = (long) sizeof(struct InputEvent);
  368.     req->io_Data = (void *) &ie;
  369.  
  370.     ie.ie_NextEvent = NULL;
  371.     ie.ie_Class = IECLASS_RAWKEY;
  372.     ie.ie_TimeStamp.tv_secs = 0;
  373.     ie.ie_TimeStamp.tv_micro = 0;
  374.     ie.ie_X = 0;
  375.     ie.ie_Y = 0;
  376.  
  377.     ModifyIDCMP(win , MOUSEBUTTONS | MOUSEMOVE | MENUPICK);
  378.  
  379.     ReportMouse(TRUE , win);
  380.  
  381.     SetMenuStrip(win , &Menu);
  382.  
  383.     while (msg = GetMsg(win->UserPort) )
  384.         ReplyMsg(msg);
  385.  
  386.  
  387.     really_open_window = 1;
  388.  
  389. loop:
  390.     WaitPort(win->UserPort);
  391.  
  392.     msg = GetMsg(win->UserPort);
  393.  
  394.     class = msg->Class;
  395.     code = msg->Code;
  396.     x = msg->MouseX;
  397.     y = msg->MouseY;
  398.  
  399.     ReplyMsg(msg);
  400.  
  401.     Forbid();
  402.  
  403.     if (y < 10)
  404.         win->Flags &= ~RMBTRAP;
  405.     else
  406.         win->Flags |= RMBTRAP;
  407.  
  408.     Permit();
  409.  
  410.     if (class == MENUPICK && code != MENUNULL)
  411.         bye();
  412.  
  413.     else if (class == MOUSEBUTTONS)
  414.     {
  415.         if (code == SELECTDOWN)
  416.         {
  417.             ry = ( (fy = y) - conUnit->cu_YROrigin) / 8;
  418.             pressed = 1;
  419.         }
  420.  
  421.         else if (code == SELECTUP)
  422.         {
  423.             prx = -1111;
  424.             px = -1111;
  425.             maxx = -1111;
  426.  
  427.             pressed = 0;
  428.         }
  429.  
  430.         else if (code == MENUDOWN)
  431.         {
  432.             rx = (x - conUnit->cu_XROrigin) / 8;
  433.             ry = (y - conUnit->cu_YROrigin) / 8;
  434.  
  435.             ie.ie_Qualifier = NULL;
  436.  
  437.             if (ry > conUnit->cu_YCCP)
  438.             {
  439.                 ie.ie_Code = 0x44;
  440.  
  441.                 DoIO(req);
  442.  
  443.                 ie.ie_Code = 0x44 | 0x80;
  444.  
  445.                 DoIO(req);
  446.             }
  447.  
  448.             else if (ry == conUnit->cu_YCCP)
  449.             {
  450.                 if (rx > conUnit->cu_XCCP)
  451.                 {
  452.                     for (i = rx - conUnit->cu_XCCP + 1 ; i ; i--)
  453.                     {
  454.                         ie.ie_Code = 0x46;
  455.  
  456.                         DoIO(req);
  457.  
  458.                         ie.ie_Code = 0x46 | 0x80;
  459.  
  460.                         DoIO(req);
  461.                     }
  462.                 }
  463.  
  464.                 else if (rx < conUnit->cu_XCCP)
  465.                 {
  466.                     for (i = conUnit->cu_XCCP - rx ; i ; i--)
  467.                     {
  468.                         ie.ie_Code = 0x41;
  469.  
  470.                         DoIO(req);
  471.  
  472.                         ie.ie_Code = 0x41 | 0x80;
  473.  
  474.                         DoIO(req);
  475.                     }
  476.                 }
  477.  
  478.                 else if (rx == conUnit->cu_XCCP)
  479.                     do_args(argc , argv);
  480.             }
  481.  
  482.             prx = -1111;
  483.             px = -1111;
  484.             maxx = -1111;
  485.  
  486.             pressed = 0;
  487.         }
  488.     }
  489.  
  490.     if (pressed && x >= conUnit->cu_XROrigin && y >= conUnit->cu_YROrigin && x <
  491.                 conUnit->cu_XRExtant && y < conUnit->cu_YRExtant)
  492.     {
  493.         rx = (x - conUnit->cu_XROrigin) / 8;
  494.  
  495.         if (rx != prx && rx > maxx)
  496.         {
  497.             maxx = rx;
  498.  
  499.             if (rx > prx && rx - prx > 1 && prx != -1111)
  500.             {
  501.                 for (i = rx - prx + 1 , x = px + 8 ; i && x <
  502.                         conUnit->cu_XRExtant ; x += 8 , i--)
  503.                     convert_char(x , fy);
  504.                 
  505.                 x -= 8;
  506.             }
  507.  
  508.             else
  509.                 convert_char(x , fy);
  510.  
  511.             rx = (x - conUnit->cu_XROrigin) / 8;
  512.  
  513.             if (rx > maxx)
  514.                 maxx = rx;
  515.  
  516.             prx = rx;
  517.  
  518.             px = x;
  519.         }
  520.     }
  521.  
  522.     goto loop;
  523.  
  524.     bye();
  525. }
  526.  
  527. bye()
  528. {
  529.     register int i;
  530.  
  531.     for (i = 0 ; i != num_of_chrs ; i++)
  532.         if (chrs[i].pln)
  533.             FreeRaster(chrs[i].pln , 8L , 8L);
  534.  
  535.     if (tbm->Planes[0])
  536.         FreeRaster(tbm->Planes[0] , 8L , 8L);
  537.  
  538.     if (conWindow && really_open_window)
  539.     {
  540.         Forbid();
  541.  
  542.         conWindow->Flags &= ~RMBTRAP;
  543.  
  544.         Permit();
  545.  
  546.         ClearMenuStrip(conWindow);
  547.  
  548.         ReportMouse(FALSE , conWindow);
  549.  
  550.         ModifyIDCMP(conWindow , NULL);
  551.     }
  552.  
  553.     if (IntuitionBase)
  554.         CloseLibrary(IntuitionBase);
  555.  
  556.     if (GfxBase)
  557.         CloseLibrary(GfxBase);
  558.  
  559.     if (NOT errdev && req)
  560.         CloseDevice(req);
  561.  
  562.     if (req)
  563.         DeleteStdIO(req);
  564.  
  565.     if (port)
  566.         DeletePort(port);
  567.  
  568.     exit(0);
  569. }
  570.  
  571. extern long sendpkt();
  572.  
  573. LONG findWindow() /* inits conWindow and conUnit (global vars) */
  574.    {
  575.    register struct InfoData *id;
  576.     register    struct MsgPort  *conid;
  577.     register    struct Process  *me;
  578.     register   LONG myargs[8] ,nargs, res1;
  579.  
  580.    /* Alloc to insure longword alignment */
  581.    id = (struct InfoData *)AllocMem( (long) sizeof(struct InfoData),
  582.                                        MEMF_PUBLIC|MEMF_CLEAR);
  583.    if(! id) return(0);
  584.    me = (struct Process *) FindTask(NULL);
  585.    conid = (struct MsgPort *) me->pr_ConsoleTask;
  586.  
  587.    myargs[0]=((ULONG)id) >> 2;
  588.    nargs = 1;
  589.    res1 = (LONG)sendpkt(conid,ACTION_DISK_INFO,myargs,nargs);
  590.    conWindow = (struct Window *)id->id_VolumeNode;
  591.    conUnit = (struct ConUnit *)
  592.                  ((struct IOStdReq *)id->id_InUse)->io_Unit;
  593.    FreeMem(id, (long) sizeof(struct InfoData));
  594.    return(res1);
  595. }
  596.  
  597.  
  598. LONG sendpkt(pid,action,args,nargs)
  599. register struct MsgPort *pid;  /* process indentifier ... (handlers message port ) */
  600. register LONG action,          /* packet type ... (what you want handler to do )   */
  601.      args[],          /* a pointer to a argument list */
  602.      nargs;           /* number of arguments in list  */
  603.    {
  604.     register    struct MsgPort        *replyport;
  605.    struct StandardPacket *packet;
  606.  
  607.     register    LONG  count, *pargs, res1;
  608.  
  609.    replyport = (struct MsgPort *) CreatePort( (long) NULL, (long) 0);
  610.    if(!replyport) return( (long) NULL);
  611.  
  612.    packet = (struct StandardPacket *) 
  613.       AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
  614.    if(!packet) 
  615.       {
  616.       DeletePort(replyport);
  617.       return(NULL);
  618.       }
  619.  
  620.    packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
  621.    packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);
  622.    packet->sp_Pkt.dp_Port         = replyport;
  623.    packet->sp_Pkt.dp_Type         = action;
  624.  
  625.    /* copy the args into the packet */
  626.    pargs = &(packet->sp_Pkt.dp_Arg1);       /* address of first argument */
  627.    for(count=0;count < nargs;count++) 
  628.       pargs[count]=args[count];
  629.  
  630.    PutMsg(pid,packet); /* send packet */
  631.  
  632.    WaitPort(replyport);
  633.    GetMsg(replyport); 
  634.  
  635.    res1 = packet->sp_Pkt.dp_Res1;
  636.  
  637.    FreeMem(packet,(long)sizeof(struct StandardPacket));
  638.    DeletePort(replyport); 
  639.  
  640.    return(res1);
  641. }
  642.